package com.icetech.eom.service.impl.tx;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.icetech.eom.dao.tx.SessionDao;
import com.icetech.eom.domain.tx.Session;
import com.icetech.eom.domain.tx.dto.SessionExcelDto;
import com.icetech.eom.service.tx.ISessionService;
import com.xuxueli.poi.excel.ExcelExportUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangzw
 * @since 2018-11-12
 */
@Service
public class SessionServiceImpl extends ServiceImpl<SessionDao, Session> implements ISessionService {
    @Autowired
    private SessionDao sessionDao;
    @Override
    public PageInfo<Session> selectByPage(String searchKey, Date beginTime, Date endTime, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Session> sessions = sessionDao.selectBySearchKey(searchKey,beginTime,endTime);
        return new PageInfo(sessions);
    }

    @Override
    public Workbook exportExcel(String searchKey) {
        List<Session> sessions = sessionDao.selectBySearchKey(searchKey, null, null);
        List<SessionExcelDto> sessionExcelDtos = Lists.newArrayList();
        sessions.forEach(session -> {
            SessionExcelDto sessionExcelDto = new  SessionExcelDto();
            BeanUtils.copyProperties(session, sessionExcelDto);
            sessionExcelDtos.add(sessionExcelDto);
        });
        return ExcelExportUtil.exportWorkbook(sessionExcelDtos);
    }
}
